<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.9.1"/>
<title>cocoOS: C:/Users/peter_000/Desktop/cocoOS/common/src/docFundamentals.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">cocoOS
   &#160;<span id="projectnumber">5.0.3</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.9.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_fundamentals_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">docFundamentals.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="doc_fundamentals_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;  system_init();</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;  </div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;  <a class="code" href="os__kernel_8c.html#a6cc2e63d83267ff5059bf0a76b302a09">os_init</a>();</div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;  </div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;  <span class="comment">/* Create kernel objects */</span></div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;  <a class="code" href="os__task_8c.html#af531a501491c2acab61a65c8741e1375">task_create</a>( myTaskProc, 0, 1, NULL, 0, 0 );</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;  mySem = <a class="code" href="os__sem_8c.html#aaecd95706696b8ff18d5ef74d139d750">sem_bin_create</a>( 1 );</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;  </div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;  <a class="code" href="os__kernel_8c.html#a19c7111cf2121a69331d99dabe3e9df0">os_start</a>();</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;  </div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;  <span class="comment">/* Will never end up here */</span></div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;  <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;}</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;<span class="keyword">@end</span>code</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;This is the preferred order of initialization. It is crucial that the clock tick is not started before the tasks</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;are created and the kernel is initialized. The <a class="code" href="os__kernel_8c.html#a19c7111cf2121a69331d99dabe3e9df0">os_start</a>() calls the macro os_enable_interrupts() which can be defined</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;to start the clock tick driving the OS.  </div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;&lt;b&gt;Time &lt;/b&gt;@n</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;cocoOS keeps track of time by counting ticks and you must feed the counting with a call to <a class="code" href="os__kernel_8c.html#a1d7bc0554f6b515785211dd2c3f2d3d9">os_tick</a>() periodically</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;from the clock tick ISR.</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;A system of one main clock and several sub clocks is used in cocoOS. The main clock is typically realized </div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;using one of the hardware timers within your target microcontroller which calls the <a class="code" href="os__kernel_8c.html#a1d7bc0554f6b515785211dd2c3f2d3d9">os_tick</a>() function that</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;decrements the timers used for task_wait(), msg_post_in() and msg_post_every().</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;If your application does not need more than a single time base, the main clock fed by the <a class="code" href="os__kernel_8c.html#a1d7bc0554f6b515785211dd2c3f2d3d9">os_tick</a>() call is</div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;all you need. @n</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;In case your application has to react to events in another time resolution than what is provided by the main</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;clock, you can use the sub clocks. The sub clocks are typically not associated with a hardware timer, but is</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;instead &quot;ticked&quot; by calling <a class="code" href="os__kernel_8c.html#ac48c31882821ef8c2acef85235ee3d13">os_sub_tick</a>(<span class="keywordtype">id</span>) from within your application code.</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;Sub clocks can also be advanced with a step greater than one using <a class="code" href="os__kernel_8c.html#a5b490624895d2f8132bce27f81e7027f">os_sub_nTick</a>(<span class="keywordtype">id</span>, nTicks). There is one</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;task function associated with the sub clocks: task_wait_id(<span class="keywordtype">id</span>,ticks). <span class="keywordtype">id</span> is a value in the range 1-255</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;assigned by the application, and ticks is the number of ticks to wait. An example of the use of sub clocks</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;could be a task that should be run after 64 bytes has been received on the UART. The task starts the wait by</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;calling task_wait_id( 1, 64 ). And in the UART rx ISR <a class="code" href="os__kernel_8c.html#ac48c31882821ef8c2acef85235ee3d13">os_sub_tick</a>(1) is called for each received character.</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;&lt;b&gt;Tasks &lt;/b&gt;@n</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;An application is built up by a number of tasks. Each task is a associated with a (preferably <span class="keywordtype">short</span>) </div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;procedure with a well defined purpose. The execution of the tasks, is managed by the os kernel, by letting</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;the highest priority task among all execution ready tasks to execute. All tasks have to make at least</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;one blocking call to a sheduling kernel function. This gives lower priority tasks a chance to execute.</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160; </div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;The task procedure must enclose its code with the task_open() and task_close() macros, as shown below. </div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;Several tasks can use the same task procedure. This is done by giving the same procedure as argument when</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;creating the tasks.</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;@code</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;static <span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;  task_open();</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;  ...</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;  ...</div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;  task_close();</div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;}</div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;<span class="keyword">@end</span>code</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;Such a task will be executed once only. If a task is intended to be executed <span class="stringliteral">&quot;for ever&quot;</span>, an endless loop</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;must be implemented.</div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;@code</div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;<span class="keyword">static</span> <span class="keywordtype">void</span> hello_task(<span class="keywordtype">void</span>) {</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;  task_open();</div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;    <span class="keywordflow">for</span>(;;) {</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;      uart_send_string(<span class="stringliteral">&quot;Hello World!&quot;</span>);</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;      task_wait( 20 );</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;    }</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;  task_close();</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;}</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;</div>
<div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;<span class="keywordtype">int</span> main(<span class="keywordtype">void</span>) {</div>
<div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;  <span class="comment">/* Setup ports, clock... */</span></div>
<div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;  system_init();</div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;  </div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;  <span class="comment">/* Create kernel objects */</span></div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;  <a class="code" href="os__task_8c.html#af531a501491c2acab61a65c8741e1375">task_create</a>( hello_task, 0, 1, NULL, 0, 0 );</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;  </div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;  <a class="code" href="os__kernel_8c.html#a6cc2e63d83267ff5059bf0a76b302a09">os_init</a>();</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;  clock_start();</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;  <a class="code" href="os__kernel_8c.html#a19c7111cf2121a69331d99dabe3e9df0">os_start</a>();</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;  </div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;  <span class="comment">/* Will never end up here */</span></div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;  <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;}</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;<span class="keyword">@end</span>code</div>
<div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;</div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;&lt;b&gt;Scheduling&lt;/b&gt;@n</div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;When a task has finished it gives the CPU control to another task by calling one of the scheduling </div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;macros:</div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160; </div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;    - task_wait()</div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;    - event_wait()</div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;    - event_wait_timeout()</div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;    - event_wait_multiple()</div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;    - event_signal()</div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;    - sem_wait()</div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;    - sem_signal()</div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;    - msg_post()</div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;    - msg_post_in()</div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;    - msg_post_every()  </div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;    - msg_receive()</div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;    </div>
<div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;Normally the scheduler will give the cpu to the highest priority task ready for execution. It is possible to </div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;choose a round robin scheduling algorithm by putting the following line in os_defines.h:@n</div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;<span class="preprocessor">#define ROUND_ROBIN</span></div>
<div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;</div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;This will make the scheduler to scan the list of tasks and run the next found task in the ready state.</div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;*/</div>
<div class="ttc" id="os__kernel_8c_html_a6cc2e63d83267ff5059bf0a76b302a09"><div class="ttname"><a href="os__kernel_8c.html#a6cc2e63d83267ff5059bf0a76b302a09">os_init</a></div><div class="ttdeci">void os_init(void)</div><div class="ttdef"><b>Definition:</b> os_kernel.c:64</div></div>
<div class="ttc" id="os__kernel_8c_html_a5b490624895d2f8132bce27f81e7027f"><div class="ttname"><a href="os__kernel_8c.html#a5b490624895d2f8132bce27f81e7027f">os_sub_nTick</a></div><div class="ttdeci">void os_sub_nTick(uint8_t id, uint32_t nTicks)</div><div class="ttdef"><b>Definition:</b> os_kernel.c:205</div></div>
<div class="ttc" id="os__kernel_8c_html_ac48c31882821ef8c2acef85235ee3d13"><div class="ttname"><a href="os__kernel_8c.html#ac48c31882821ef8c2acef85235ee3d13">os_sub_tick</a></div><div class="ttdeci">void os_sub_tick(uint8_t id)</div><div class="ttdef"><b>Definition:</b> os_kernel.c:176</div></div>
<div class="ttc" id="os__task_8c_html_af531a501491c2acab61a65c8741e1375"><div class="ttname"><a href="os__task_8c.html#af531a501491c2acab61a65c8741e1375">task_create</a></div><div class="ttdeci">uint8_t task_create(taskproctype taskproc, void *data, uint8_t prio, Msg_t *msgPool, uint8_t poolSize, uint16_t msgSize)</div><div class="ttdef"><b>Definition:</b> os_task.c:134</div></div>
<div class="ttc" id="os__kernel_8c_html_a1d7bc0554f6b515785211dd2c3f2d3d9"><div class="ttname"><a href="os__kernel_8c.html#a1d7bc0554f6b515785211dd2c3f2d3d9">os_tick</a></div><div class="ttdeci">void os_tick(void)</div><div class="ttdef"><b>Definition:</b> os_kernel.c:149</div></div>
<div class="ttc" id="os__kernel_8c_html_a19c7111cf2121a69331d99dabe3e9df0"><div class="ttname"><a href="os__kernel_8c.html#a19c7111cf2121a69331d99dabe3e9df0">os_start</a></div><div class="ttdeci">void os_start(void)</div><div class="ttdef"><b>Definition:</b> os_kernel.c:121</div></div>
<div class="ttc" id="os__sem_8c_html_aaecd95706696b8ff18d5ef74d139d750"><div class="ttname"><a href="os__sem_8c.html#aaecd95706696b8ff18d5ef74d139d750">sem_bin_create</a></div><div class="ttdeci">Sem_t sem_bin_create(uint8_t initial)</div><div class="ttdef"><b>Definition:</b> os_sem.c:95</div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_bdd9a5d540de89e9fe90efdfc6973a4f.html">common</a></li><li class="navelem"><a class="el" href="dir_4e4d22e38ca4e2e8207825ee2d00bc54.html">src</a></li><li class="navelem"><a class="el" href="doc_fundamentals_8h.html">docFundamentals.h</a></li>
    <li class="footer">Generated on Tue Aug 28 2018 17:58:05 for cocoOS by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.9.1 </li>
  </ul>
</div>
</body>
</html>
